# -*- coding:utf-8 -*-

from common.import_pyside import *
from common.data import DB
import os


class Side(QWidget):
    def __init__(self, parent=None):
        super(Side, self).__init__(parent=parent)
        v_layout = QVBoxLayout()
        v_layout.setAlignment(Qt.AlignmentFlag.AlignTop)

        self.side_search_layout = QHBoxLayout()
        self.side_search_line = QLineEdit()
        self.side_search_line.setPlaceholderText("输入任务名称查询")
        self.side_search_line.textChanged.connect(self.search_line_textChange)
        self.side_search_button = QPushButton("清除", self)
        self.side_search_button.clicked.connect(self.side_search_line.clear)
        self.side_search_layout.addWidget(self.side_search_line)
        # search_laout.addStretch(1)
        self.side_search_layout.addWidget(self.side_search_button)

        self.side_list_widget = QListWidget()
        # for i in range(10):
        #     ql = QListWidgetItem()
        #     bu = QPushButton(f"23")
        #     list_widget.insertItem(list_widget.count(),"121223232323")
        #     list_widget.setItemWidget(ql, bu)
        # for item in os.listdir(os.path.join(DB.ABSPATH, DB.DirName)):
        #     self.side_list_widget.addItem(item)
        self.side_list_widget.currentItemChanged.connect(self.side_list_widget_currentChange)

        v_layout.addLayout(self.side_search_layout)
        v_layout.addWidget(self.side_list_widget)
        self.setLayout(v_layout)

    def search_line_textChange(self):
        # 先清除
        self.side_list_widget.clear()
        # 再添加
        for item in os.listdir(os.path.join(DB.ABSPATH, DB.DirName)):
            self.side_list_widget.addItem(item)

    def side_list_widget_currentChange(self):
        print(self.side_list_widget.currentItem().text())
        DB.case_task_name = self.side_list_widget.currentItem().text()

        # print(self.side_list_widget.currentItem())
        # print(self.side_list_widget.currentRow())
        # print(self.side_list_widget.currentIndex())
        # PySide6.QtWidgets.QListWidgetItem
        # 4
        # PySide6.QtCore.QModelIndex(4, 0, 0x2b651ed8680, QListModel(0x2b652981110))
